package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.entity.VideoGift;
import com.wyh.mapper.VideoGiftMapper;
import com.wyh.request.VideoGiftRequest;
import com.wyh.service.IVideoGiftService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 礼物管理业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class VideoGiftServiceImpl implements IVideoGiftService {

    private final VideoGiftMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VideoGift add(VideoGiftRequest request) {
        VideoGift entity = VideoGift.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(VideoGiftRequest request) {
        VideoGift entity = VideoGift.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public VideoGift getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<VideoGift> pageList(VideoGiftRequest request) {
        Page<VideoGift> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<VideoGift> query = this.buildWrapper(request);
        IPage<VideoGift> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<VideoGift> list(VideoGiftRequest request) {
        LambdaQueryWrapper<VideoGift> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<VideoGift> buildWrapper(VideoGiftRequest param) {
        LambdaQueryWrapper<VideoGift> query = new LambdaQueryWrapper<>();
        if (param.getGiftId() != null){
               query.eq(VideoGift::getGiftId ,param.getGiftId());
        }
         if (StringUtils.isNotBlank(param.getGiftName())){
               query.like(VideoGift::getGiftName ,param.getGiftName());
        }
         if (StringUtils.isNotBlank(param.getGiftDescription())){
               query.like(VideoGift::getGiftDescription ,param.getGiftDescription());
        }
         if (param.getGiftValue() != null){
               query.like(VideoGift::getGiftValue ,param.getGiftValue());
        }
         if (StringUtils.isNotBlank(param.getGiftImage())){
               query.like(VideoGift::getGiftImage ,param.getGiftImage());
        }
        if (param.getIsDelete() != null){
               query.eq(VideoGift::getIsDelete ,param.getIsDelete());
        }
        if (param.getCreateUserId() != null){
               query.eq(VideoGift::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(VideoGift::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
